<?php

/**
 * This is the model class for table "{{material_warehouse_inbound}}".
 *
 * The followings are the available columns in table '{{material_warehouse_inbound}}':
 * @property integer $id
 * @property integer $projectId
 * @property string $submitDate
 * @property string $token
 * @property string $deliveryNumber
 * @property string $note
 * @property string $handler
 * @property integer $warehouseManagerId
 * @property string $createTime
 * @property integer $createUserId
 * @property string $updateTime
 * @property integer $updateUserId
 *
 * The followings are the available model relations:
 * @property Employee $warehouseManager
 * @property User $createUser
 * @property Project $project
 * @property User $updateUser
 * @property MaterialWarehouseInboundItem[] $items
 */
class MaterialWarehouseInbound extends CActiveRecord {
	/**
	 * Returns the static model of the specified AR class.
	 * @return MaterialWarehouseInbound the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	public static function getPopularHandlers() {
		$id = __CLASS__ . '.handler.popular';
		$value = Yii::app()->cache->get($id);
		if ($value === false) {
			$criteria = new CDbCriteria();
			$criteria->distinct = true;
			$criteria->select = 'handler';
			$criteria->order = 'count(*) desc';
			$criteria->limit = 8;
			$criteria->group = 'handler';
			$handlers = MaterialWarehouseInbound::model()->findAll($criteria);
			$value = array();
			foreach ($handlers as $handler) {
				$value[] = $handler->handler;
			}
			Yii::app()->cache->set($id, $value, YII_DEBUG ? 1 : 60);
		}
		return $value;
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return '{{material_warehouse_inbound}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(
				'projectId, warehouseManagerId, token, handler, submitDate',
				'required'
			),
			array(
				'token',
				'match',
				'pattern'=>'/^([a-zA-Z]{0,2})?\d{6,8}$/',
				'message'=>'{attribute} 应该是0-2位字母和6-8位数字',
			),
			array(
				'projectId, warehouseManagerId',
				'numerical',
				'integerOnly' => true
			),
			array(
				'token, note, deliveryNumber, handler',
				'length',
				'max' => 255
			),
			array(
				'submitDate',
				'date',
				'format' => 'yyyy-MM-dd'
			),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array(
				'id, projectId, submitDate, token, deliveryNumber, note, handler, warehouseManagerId, createTime, createUserId, updateTime, updateUserId',
				'safe',
				'on' => 'search'
			),
		);
	}

	public function behaviors() {
		return array(
			'AuditableBehavior' => array(
				'class' => 'components.db.AuditableBehavior',
				'attributes' => 'projectId, submitDate, token, note, handler, warehouseManagerId, deliveryNumber'
			),
			'AuditStampBehavior' => array(
				'class' => 'components.db.AuditStampBehavior',
			),
			'IsRecentBehavior' => array(
				'class' => 'components.models.IsRecentBehavior',
			),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'warehouseManager' => array(
				self::BELONGS_TO,
				'Employee',
				'warehouseManagerId'
			),
			'createUser' => array(
				self::BELONGS_TO,
				'User',
				'createUserId'
			),
			'project' => array(
				self::BELONGS_TO,
				'Project',
				'projectId'
			),
			'updateUser' => array(
				self::BELONGS_TO,
				'User',
				'updateUserId'
			),
			'items' => array(
				self::HAS_MANY,
				'MaterialWarehouseInboundItem',
				'materialWarehouseInboundId',
				'with'=>'materialModel.material'
			),
			'itemsQuick' => array(
				self::HAS_MANY,
				'MaterialWarehouseInboundItem',
				'materialWarehouseInboundId',
				'limit'=>3,
				'with'=>'materialModel.material'
			),
			'itemCount' => array(
				self::STAT,
				'MaterialWarehouseInboundItem',
				'materialWarehouseInboundId'
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id' => 'ID',
			'projectId' => '项目',
			'submitDate' => '入库时间',
			'token' => '入库单号',
			'deliveryNumber'=>'送货单号',
			'note' => '备注',
			'handler' => '经手人',
			'warehouseManagerId' => '库管',
			'createTime' => '创建时间',
			'createUserId' => '创建用户',
			'updateTime' => '更新时间',
			'updateUserId' => '更新用户',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id);
		$criteria->compare('projectId', $this->projectId);
		$criteria->compare('submitDate', $this->submitDate, true);
		$criteria->compare('token', $this->token, true);
		$criteria->compare('deliveryNumber', $this->deliveryNumber, true);
		$criteria->compare('note', $this->note, true);
		$criteria->compare('handler', $this->handler, true);
		$criteria->compare('warehouseManagerId', $this->warehouseManagerId);
		$criteria->compare('createTime', $this->createTime, true);
		$criteria->compare('createUserId', $this->createUserId);
		$criteria->compare('updateTime', $this->updateTime, true);
		$criteria->compare('updateUserId', $this->updateUserId);
		$criteria->with = array(
			'project'
		);

		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
		));
	}
}
